package com.lzy.util;

import com.lzy.model.RestResponseResult;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Optional;

/**
 * @author lzy by 2019/9/11 16:05
 */
public class ResultThreadLocalUtil {
    private static HashMap getHashMap() {
        return Optional.ofNullable(ThreadLocalUtil.getObject()).map(o -> (HashMap) o).orElse(new HashMap());
    }

    public static void setRestResponse(RestResponseResult restResponseResult) {
        HashMap hashMap = getHashMap();
        if (CollectionUtils.isEmpty(hashMap)) {
            hashMap.put("restResponse", restResponseResult);
            ThreadLocalUtil.setObject(hashMap);
        }
        hashMap.put("restResponse", restResponseResult);
    }

    public static RestResponseResult getRestResponse() {
        HashMap hashMap = getHashMap();
        return (RestResponseResult) hashMap.get("restResponse");
    }


}
